package cn.bocweb.spinning.view.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.BindColor;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.MyClassNewBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.view.adapter.MySchoolListAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.GlideCircle;
import de.greenrobot.event.EventBus;

public class MySchoolListActivity extends BaseActivity {

    MySchoolListAdapter mySchoolListAdapter;
    UserPresenter presenter;
    String userid;
    List<MyClassNewBean.MatchBean.RecordBean> matchBeen;
    List<BestBean> bestBeen;
    List<MyClassNewBean.RecordBean> recordBeen;
    List<String> titles;

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.listview_main)
    ExpandableListView listviewMain;
    @BindColor(R.color.white)
    int white;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_school_list);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);

        initToolbar();
        initListView();
        initData();
        showLoading();
    }

    private void initToolbar() {
        toolbar.setTitle("我的学堂");
        toolbar.setTitleTextColor(white);
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(this);
        userid = dataEntity.getUserid();

        presenter = new UserPresenterImp();
        presenter.myClassNew(userid);
    }

    public void onEventMainThread(MyClassNewBean bean) {
        hideLoading();
        if (bean.getReturn_code() == 0) {
            titles.add(bean.getMatch().getTitle());
            titles.add("学习进度");
            titles.add("最好成绩");
            if (bean.getMatch() != null) {
                //完成比赛列表
                matchBeen.addAll(bean.getMatch().getRecord());
            }
            if (bean.getRecord() != null) {
                recordBeen.addAll(bean.getRecord());

                //完成最好成绩列表
                for (MyClassNewBean.RecordBean recordBean : bean.getRecord()) {
                    bestBeen.add(new BestBean().copyBest(recordBean.getBest()).setTitle(recordBean.getTitle()));
                }
                mySchoolListAdapter.notifyDataSetChanged();
            }
        }
    }

    private void initListView() {
        titles = new ArrayList<>();
        matchBeen = new ArrayList<>();
        recordBeen = new ArrayList<>();
        bestBeen = new ArrayList<>();
        mySchoolListAdapter = new MySchoolListAdapter(titles, matchBeen, recordBeen, bestBeen);
        listviewMain.setAdapter(mySchoolListAdapter);
        listviewMain.setGroupIndicator(null);

        addHeader();
    }

    private void addHeader() {
        View view = LayoutInflater.from(this).inflate(R.layout.header_school, null);

        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(this);
        Glide
                .with(this)
                .load(dataEntity.getUthumb())
                .placeholder(R.mipmap.login_head)
                .transform(new GlideCircle(this))
                .into((ImageView)view.findViewById(R.id.my_icon));

        ((TextView)view.findViewById(R.id.my_name)).setText(dataEntity.getNickname());
        ((TextView)view.findViewById(R.id.my_username)).setText(dataEntity.getPhone());
        ((TextView)view.findViewById(R.id.my_realname)).setText(dataEntity.getRealname());
        listviewMain.addHeaderView(view);
    }
}
